from logging import debug
from threading import main_thread
from flask import request, Blueprint
from flask.json import jsonify
from flask_cors import CORS
from db import Db
from db1 import Dbb,make_password
import re


hhh = Blueprint('hhh',__name__)


@hhh.route('/getauthor/',methods=['GET'])
def getauthor():
    id = request.args.get('id')
    count = request.args.get('count')
    print(type(count))
    db = Db()
    if id:
        res = db.get_one("select `views` from `author` where id=%s"%id)
        res = res['views']
        if res ==0:
            res= 1
        else:
            res+=1
        db.update("update author set views='%s' where id=%s"%(res,id))  
    else:
        res = db.get_all("select * from `author`")
    return jsonify(res)

@hhh.route('/getbook/',methods=['GET'])
def getbook():
    db = Db()
    res = db.get_all("select a.name,a.id,a.views,b.id as bid, b.name as authorname from `books` a left join `author` b on a.aid = b.id")
    return jsonify(res)

@hhh.route('/register1/',methods=['POST'])
def register1():
    username = request.form.get('username','')
    name = request.form.get('name','')
    password = request.form.get('password','')
    password1 = request.form.get('password1','')
    db = Dbb()
    print(username,name,password1,password)
    if not username:
        return {'code':10010,'msg':'用户名为空'}
    if not name:
        return {'code':10011,'msg':'账号为空'}
    res = db.get_one("select `username` from `uuser` where username = '%s'"%username)
    if res:
        return {'code':10012,'msg':'该账号已存在'}
    pwd = re.match('\d{1,}\w{1,}',password)
    if not pwd:
        return {'code':10013,'msg':'密码太简单'}
    if password !=password1:
        return {'code':10014,'msg':'两次密码不一致'}
    else:
        pwd2 = make_password(password)
        try:
            db.insert("insert into `uuser` (name,username,password) values ('%s','%s','%s')"%(name,username,pwd2))
            res = db.get_one("select * from `uuser` where name='%s'"%name)
        except Exception as e:
            print(e)
            return {'code':400,'msg':'添加失败'}
        else:
            return {'code':200,'msg':'添加成功','userid':res['id'],'username':username}

import hashlib
@hhh.route('/people_register/',methods=['POST'])
def people_register():
    username = request.form.get('username','')
    password = request.form.get('password','') 
    if not username:
        return {'code':10010,'msg':'用户名为空'}
    db = Dbb()
    res =db.get_one("select `name` from `people` where name = '%s'"%username)  
    if res:
        return {'code':10011,'msg':'该用户已存在'}
    pwd1 = re.match('^(?=.*\d+.*)(?=.*[a-z]+.*)(?=.*_.*)[\da-z_]{6,}$',password)
    if  not pwd1:
        return {'code':10012,'msg':'密码不符合规范'}
    md5 = hashlib.md5()
    str1 = str(password).encode('utf-8')
    md5.update(str1)
    pwd = md5.hexdigest()
    db.insert("insert into `people`(name,password) values('%s','%s')"%(username,pwd))
    return {'code':200}


from flask import redirect,url_for
#测试重定向
@hhh.route('/ren/',methods=['GET'])
def ren():
    name = '123'
    return redirect('http://localhost:8080/mylogin')

